package offerbook;

/**
 * 有两个数m和n，判断m需要改变几位才能变成n.
 * 即汉明距离：譬如 2 = 010， 3 = 101.
 * 2和3不同bit位就是答案。
 * 即统计不同bit位个数
 *
 * @date 2020/3/8 16:05
 */
public class Code10_HanMingDistance {
    public static int hanMingDistance(int n, int m) {
        int diff = (m ^ n);//不同的bit位为1，统计diff中1的个数
        int res = 0;
        while (diff != 0) {
            diff = (diff & (diff - 1));
            res++;
        }
        return res;
    }
    public static int getCount(int m, int n) {
        int count = 0;
        int res = m ^ n;
        /**
         * 统计res中1的个数
         */
        while (res != 0) {
            res = (res & (res - 1));
            count++;
        }

        return count;
    }
    public static void main(String[] args) {
        System.out.println(getCount(2, 5));
        System.out.println(hanMingDistance(2, 5));
    }
}
